package com.hehua.SingletonTwo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class LoaderBalancer {
    //私有化静态成员变量,储存唯一实例
    private static LoaderBalancer loaderBalancer = null;
    //服务器集合
    private List serverList = null;

    //私有化构造参数
    private LoaderBalancer(){
        serverList = new ArrayList();
    }

    //公共静态成员方法,返回唯一实例
    public static LoaderBalancer getLoaderBalancer(){
        if (loaderBalancer == null){
            loaderBalancer = new LoaderBalancer();
        }
        return loaderBalancer;
    }

    //增加服务器
    public void addServer(String server){
        serverList.add(server);
    }
    //删除服务器
    public void removeServer(String server){
        serverList.remove(server);
    }
    //使用Random类随机获取服务器
    public String getServer(){
        Random random = new Random();
        int i = random.nextInt(serverList.size());
        return (String)serverList.get(i);
    }
}
